home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 3336 < prev    next >
Encoding:
Text File  |  1996-08-05  |  2.3 KB  |  72 lines

  1. Path: port6.sniff.smallmedia.com!user
  2. From: mhoward@plainfield.bypass.com (Mark Howard)
  3. Newsgroups: comp.lang.c
  4. Subject: Newbie - I'm stuck
  5. Date: Sat, 27 Jan 1996 18:16:57 -0500
  6. Organization: TGF Internet Services
  7. Message-ID: <mhoward-2701961816570001@port6.sniff.smallmedia.com>
  8. NNTP-Posting-Host: port3.sniff.smallmedia.com
  9.  
  10. Hi all,
  11. I'm stuck. The problem was: Write a program that takes a 32 bit int and
  12. splits it into 8 4-bit values. What I've got seems to work for the first
  13. 16 bits in num, but then stops recognizing set bits. I've stepped through
  14. this a bunch of times in the debugger but I'm still clueless!
  15.  
  16. Thanks, Mark
  17.  
  18. ---- program that doesn't work -----
  19. #include        <stdio.h>
  20.  
  21. int     main(void)
  22. {
  23.         char           line[20];
  24.         unsigned long  num;                       /* our long int to split */
  25.         int            cntr1, cntr2, bitCntr;   /* counters */
  26.         char           halfbyt[8];                             
  27.                                 
  28.         printf("Enter a number: ");                             /* Get a num */
  29.         fgets(line, sizeof(line), stdin);
  30.         sscanf(line, "%lu", &num);
  31.         
  32.         printf("You Entered (decimal) --> %lu\n", num); /* Display what
  33. you got*/
  34.         printf("You Entered (hex) --> %#.8lx\n", num); /* Display num as hex */
  35.         
  36.         cntr1 = 0;
  37.         bitCntr = 0;
  38.         while(bitCntr < 32)
  39.         {
  40.                 halfbyt[cntr1] = NULL;  /* clear each char before setting
  41. the bits */
  42.                 for(cntr2 = 0; cntr2 < 4; cntr2 ++)
  43.                 {
  44.                         if((num & (1 << bitCntr)) != 0)   /* if bit in num
  45. is setè*/
  46.                                 /* set corresponding bit in halfbyt[] */
  47.                                 halfbyt[cntr1] |= (1 << cntr2); 
  48.                         bitCntr ++;
  49.                 }
  50.                 cntr1 ++;
  51.         }
  52.         
  53.         printf("Split:\n");
  54.         for(cntr1 = 0; cntr1 < 8; cntr1 ++)
  55.                 printf("Half Byte %d --> %#x\n", cntr1, halfbyt[cntr1]);
  56. }
  57.                 
  58.  
  59. ----------  sample Output ----------------------
  60. Enter a number: 1234567
  61. You Entered (decimal) --> 1234567
  62. You Entered (hex) --> 0x0012d687
  63. Split:
  64. Half Byte 0 --> 0x7
  65. Half Byte 1 --> 0x8
  66. Half Byte 2 --> 0x6
  67. Half Byte 3 --> 0xd
  68. Half Byte 4 --> 0       --- should be 0x2
  69. Half Byte 5 --> 0       --- should be 0x1
  70. Half Byte 6 --> 0
  71. Half Byte 7 --> 0
  72.